<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$mapping_id = mysql_real_escape_string(strip_tags($_POST['id']));

$get_bname = "SELECT * FROM buildings b JOIN building_subject_mapping bsm ON bsm.building_id = b.id WHERE bsm.id='$mapping_id'";
$result_bname = mysql_query($get_bname);
$building_name = mysql_result($result_bname,0,"building_name");

$get_sname = "SELECT * FROM course_subjects cs JOIN building_subject_mapping bsm ON bsm.course_subject_id = cs.id WHERE bsm.id='$mapping_id'";
$result_sname = mysql_query($get_sname);
$subject_name = mysql_result($result_sname,0,"subject_title");
$subject_id = mysql_result($result_sname,0,"course_subject_id");

$get_priority = "SELECT * FROM building_subject_mapping WHERE id = $mapping_id";
$result_priority = mysql_query($get_priority);
$priority = mysql_result($result_priority,0,"priority");

echo "<h1>Delete Classroom in $building_name</h1>";

$get_num = mysql_query("SELECT * FROM building_subject_mapping WHERE course_subject_id = $subject_id ORDER BY priority");
$num_rows=mysql_numrows($get_num);


$i = 0;

while ($i < $num_rows)
{
	$pri2=mysql_result($get_num,$i,"priority");
	$id2=mysql_result($get_num,$i,"id");
		
	if ($pri2 > $priority)
	{
		$pri2--;
		
		$result=mysql_query("UPDATE building_subject_mapping
			SET priority='$pri2' WHERE id='$id2'");
	}
	
	$i++;
}

$query = "DELETE FROM building_subject_mapping WHERE id='$mapping_id'";
$result = mysql_query($query);

if ($result)
{
	echo "<p>";
	echo "Subject to building mapping between <b>$subject_name</b> and <b>$building_name</b> has been deleted!";
	echo "<p>";
}
else
{
	echo "<p>";
	echo "Subject to building mapping between <b>$subject_name</b> and <b>$building_name</b> was NOT able to be deleted at this time!";
	echo "<p>";
}

//close
mysql_close();

echo "<p>";
	echo "<a href='index.php?id=$subject_id'>&lt;&lt; Back to Subject/Building Mapping Portal</a><br />";
echo "</p>";
?>

<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>